package com.github.postsanf.yinian.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.LoMoPreViewAdapter;
import com.github.postsanf.yinian.bean.ItemDetail;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;
import com.umeng.analytics.MobclickAgent;

import java.util.LinkedHashMap;
import java.util.List;

public class TimeGoodsLomoPreViewActivity extends BaseActivity implements View.OnClickListener {
    private ImageView timegood_back;
    private TextView tv_sure;
    private GridView gd_lomo;
    private ProgressDialog mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time_goods_lomo_preview);
        initData();
    }

    private void initData() {
        mProgressDialog = ProgressDialog.show(TimeGoodsLomoPreViewActivity.this, "", "正在加载中...");
        String itemID = getIntent().getStringExtra("itemID");
        if (itemID == null) {
            showToast("信息加载出错请稍后重试...");
            return;
        }
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_ITEM_ID, itemID);
        StringRequest req = new StringRequest(getString(R.string.SHOP_URL) + getString(R.string.ynGetItemDetail))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        ItemDetail itemDetail = gson.fromJson(s, ItemDetail.class);
                        if (itemDetail.getMsg().equals("success")) {
                            List<String> itemPic = itemDetail.getData().get(0).getItemPic();
                            initView(itemPic);
                        } else {
                            showToast(itemDetail.getMsg());
                        }
                        mProgressDialog.dismiss();
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        showToast(CommonConstants.YN_NET_LINK);
                        mProgressDialog.dismiss();
                    }
                });
        liteHttp.executeAsync(req);
    }

    private void initView(List<String> itemPic) {
        timegood_back = ((ImageView) findViewById(R.id.timegood_back));
        tv_sure = ((TextView) findViewById(R.id.tv_sure));
        gd_lomo = ((GridView) findViewById(R.id.gd_lomo));
        timegood_back.setOnClickListener(this);
        tv_sure.setOnClickListener(this);
        LoMoPreViewAdapter loMoAdapter = new LoMoPreViewAdapter(TimeGoodsLomoPreViewActivity.this, itemPic);
        gd_lomo.setAdapter(loMoAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.timegood_back:
                finish();
                break;
        }
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
